package com.ibm.android.camera;

import android.hardware.Camera;
import android.hardware.Camera.Size;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

public class PreviewCallback implements Camera.PreviewCallback {
	private static final String TAG = PreviewCallback.class.getSimpleName();

	private ICamera mCamera;
	private Handler mHandler;
	private int mHandlerMessage;

	private boolean mUseOneShotPreviewCallBack;

	public PreviewCallback(ICamera camera, boolean useOneShotPreviewCallback) {
		mCamera = camera;
		mUseOneShotPreviewCallBack = useOneShotPreviewCallback;
	}

	void setHandler(Handler handler, int message) {
		mHandler = handler;
		mHandlerMessage = message;
	}

	@Override
	public void onPreviewFrame(byte[] data, Camera camera) {
		Size cameraResolution = mCamera.getParameters().getPreviewSize();
		mCamera.setPreviewCallback(null);
		Handler h = mHandler;
		if (h != null) {
			Message message = mHandler.obtainMessage(mHandlerMessage,
					cameraResolution.width, cameraResolution.height, data);
			message.sendToTarget();
			if (mUseOneShotPreviewCallBack)
				mCamera.setPreviewCallback(null);
		} else {
			Log.d(TAG, "Got preview callback, but no handler for it");
		}
	}

}
